let db = wx.cloud.database()

Page({
  data:{
    recipeTypeList:[],//存储菜谱分类
    files:[],//用户选择要上传的图片，array[Object] 同时object必须要含有一个正确的url图片地址
  },
  //用户选择图片 然后修改files变量
  _chooseImageHandle(e){
    //console.log(e,'选择')
    let {tempFilePaths} = e.detail
    //forEach  for()  while : map
    let files = tempFilePaths.map(item=>{ //处理成 [ { url:'' } ]
      return { url:item }
    })
    //console.log( files )
    this.setData({
      files:this.data.files.concat(files) ,//把原先有的数据和新选择的图片连接起来
    })
  },
  _deleteImageHandle(e){
    //console.log(e,'删除')
    let index = e.detail.index
    this.data.files.splice(index,1)
    //this.setData()
  },
  onLoad(){
    this._getAllRecipeTypes()
  },
  async _getAllRecipeTypes(){
    let recipeResult = await db.collection('re-recipeType').where({
      status:1
    }).get()
    // console.log( recipeResult )
    this.setData({
      recipeTypeList:recipeResult.data
    })
  },
  //发布菜谱做法
  async _doPublistRecipe(e){
    //console.log(e.detail.value)
    /*
    recipeName 菜谱名称 String
    recipeTypeId 菜谱所属分类的id String
    recipeInfo 菜谱描述 String
    
    recipeImgs 菜谱图片 ['http:/1.jpg','http:2.jpg]
    views:查看次数 Number
    fav:收藏个数 Number
    addtime:添加时间  new Date()
    status:通用属性  1正常  2删除
    */

    // console.log(e.detail.value,'1111')
    let {recipeTypeId} = e.detail.value
    let {_openid} = wx.getStorageSync('userInfo')
    let userPbType = await db.collection('re-userPbTypes').where({
      _openid,
      recipeTypeId
    }).get()
    // console.log(userPbType,'不过')
    // ------以下是往re-userPbTypes表存储用户往哪个菜谱分类下发布了菜谱
    if(userPbType.data.length == 0){
      db.collection('re-userPbTypes').add({
        data:{
          recipeTypeId
        }
      })
    }
    // return

    let data = e.detail.value //recipeName  recipeTypeId  recipeInfo
    data.views = 0
    data.fav = 0
    data.addtime = new Date()
    data.status = 1//默认正常
    //调用文件上传功能
    let uploads = await this._doUploader( this.data.files ) //虽然函数返回并处理await，但是接收还是拿到了一个promise
    // console.log( uploads,'返回给调用者' )
    data.recipeImgs = uploads.map(item=>{
        return item.fileID
    })
    
    //console.log(data,'ok111')
    let addresult = await db.collection('re-recipes').add({
      data
    })
    console.log( addresult,'添加成功' )
    if( addresult._id ){
      wx.showToast({
        title: '添加成功',
      })
    }
   
  },
  //封装的多文件上传功能
  async _doUploader( files ){
    //this.data.files 
    let uploaders = []//把每一个promisepush到uploaders组成 一个promises数组
    //Promise.all( array[promise] )
    files.forEach(item=>{

      let fileNames = item.url.split('.')
      // console.log(fileNames)
      let suffixName = fileNames[fileNames.length - 1]
      //console.log( suffixName )
      let name = Math.random().toString().substring(2) + '.' +suffixName

       let p = wx.cloud.uploadFile({
          cloudPath:name,//上传文件路径
          filePath:item.url,//要上传的文件的临时路径
      })
      uploaders.push( p )//把每一个promisepush到uploaders组成 一个promises数组
    })
    
    // console.log( upResult,'上传' )
    return await Promise.all( uploaders  )
   // console.log( results,'ok123' )
  }
})